# -*- coding: utf-8 -*-

from django.db import models

from basic_info.models import Stock

# 新闻相关模型定义

class NewsEdition():
    """
    当日新闻版本
    """
    # 早报
    MORNING = 0
    # 晚报
    EVENING = 1

    Edition = (
            (MORNING, 'Morning edition'),
            (EVENING, 'Evening edition'),
    )

class NewsCategory():
    """
    当日新闻类型
    """
    # 头版
    HEADLINE = 'touban'
    # 要闻
    FRONTPAGE = 'yaowen'
    # 晨报
    MORNING_REPORT = 'chenbao'
    # 个股
    STOCK_NEWS = 'gegu'
    # 公告
    ANNOUCEMENT = 'gonggao'
    # 行业新闻
    INDUSTRY_NEWS = 'hangye_news'
    # 行业研报
    INDUSTRY_RESEARCH = 'hangye_yanbao'

    Category = (
            (HEADLINE, '头版'),
            (FRONTPAGE, '要闻'),
            (MORNING_REPORT, '晨报'),
            (STOCK_NEWS, '个股'),
            (ANNOUCEMENT, '公告'),
            (INDUSTRY_NEWS, '行业新闻'),
            (INDUSTRY_RESEARCH, '要闻'),
    )

class DailySummary(models.Model):
    """
    当日新闻综述 
    """
    # 发布日期
    date = models.DateField()
    # 类别
    category = models.CharField(choices=NewsCategory.Category, max_length=10)
    # 版本
    edition = models.IntegerField(choices=NewsEdition.Edition, default=NewsEdition.MORNING)
    # 最热关键词
    top_keywords = models.CharField(max_length=200, blank=True)
    # 新闻条数
    doc_count = models.IntegerField(default=0)
    # 正面条数
    positive_count = models.IntegerField(default=0)
    # 负面条数
    negative_count = models.IntegerField(default=0)
    # 中性条数
    neutral_count = models.IntegerField(default=0)
    # 爬取链接数
    domain_count = models.IntegerField(default=0)

    class Meta:
        unique_together = ('date', 'category', 'edition')

class NewsDetail(models.Model):
    """
    当日新闻详情 
    """
    # 发布日期
    date = models.DateField(db_index=True)
    # 类别
    category = models.CharField(choices=NewsCategory.Category, max_length=10)
    # 新闻链接哈希
    url_hash = models.CharField(max_length=32, db_index=True)
    # 新闻链接
    url = models.URLField(max_length=200)
    # 标题
    title = models.CharField(max_length=50)
    # 发布者
    publisher = models.CharField(max_length=100)
    # 发布时间
    published_time = models.DateTimeField()
    # 关键词
    keywords = models.CharField(max_length=200, blank=True)
    # 正负倾向
    polarity = models.IntegerField(default=-1)
    # 静态摘要
    static_summary = models.TextField(blank=True)
    # 涉及股票
    related_stocks = models.ManyToManyField(Stock, through='NewsStocksMap')
    # 成功入库
    add_succeeded = models.BooleanField()

    def __unicode__(self):
        return self.title

    class Meta:
        unique_together = ('date', 'category', 'url_hash')
        index_together = [
            ['date', 'category'],
            ['date', 'add_succeeded'],
        ]

    @staticmethod
    def get_processsed_news_url_hash_set(category, date):
        """
        返回已处理过的新闻链接哈希
        """
        processed_news_url_hash_set = set()
        news_detail_set = NewsDetail.objects.filter(date=date, category=category)
        for news_detail in news_detail_set:
            processed_news_url_hash_set.add(news_detail.url_hash)
        return processed_news_url_hash_set

class NewsStocksMap(models.Model):
    """
    每篇新闻提及的股票 
    """
    news = models.ForeignKey(NewsDetail)
    stock = models.ForeignKey(Stock)

    class Meta:
        unique_together = ('news', 'stock')

class NewsCluster(models.Model):
    """
    当日新闻聚类
    """
    # 发布日期
    date = models.DateField(db_index=True)
    # 聚类id
    cluster_id = models.IntegerField()
    # 新闻
    news = models.ForeignKey(NewsDetail)

    class Meta:
        unique_together = ('date', 'cluster_id', 'news')

class ClusterSummary(models.Model):
    """
    当日每聚类纪要
    """
    # 发布日期
    date = models.DateField(db_index=True)
    # 聚类id
    cluster_id = models.IntegerField()
    # 聚类关键词
    keywords = models.CharField(max_length=200, blank=True)
    # 新闻条数
    doc_count = models.IntegerField(default=0)
    # 正面条数
    positive_count = models.IntegerField(default=0)
    # 负面条数
    negative_count = models.IntegerField(default=0)
    # 中性条数
    neutral_count = models.IntegerField(default=0)
    # 本聚类展示新闻
    display_news = models.ForeignKey(NewsDetail)

    class Meta:
        unique_together = ('date', 'cluster_id')

class StockSummary(models.Model):
    """
    当日个股新闻纪要
    """
    stock = models.ForeignKey(Stock)
    # 发布日期
    date = models.DateField()
    # 版本
    edition = models.IntegerField(choices=NewsEdition.Edition, default=NewsEdition.MORNING)
    # 新闻条数
    doc_count = models.IntegerField(default=0)
    # 正面条数
    positive_count = models.IntegerField(default=0)
    # 负面条数
    negative_count = models.IntegerField(default=0)
    # 中性条数
    neutral_count = models.IntegerField(default=0)

    def __unicode__(self):
        return self.stock.title

    class Meta:
        unique_together = ('stock', 'date', 'edition')

